#ifndef NSSHARE_UTILS_JOB_H_
#define NSSHARE_UTILS_JOB_H_


#include <queue>
#include "../sys/event.h"
#include "../sys/semaphore.h"
#include "../sys/spin_lock.h"

namespace nsshare
{
namespace utils
{

class Job
{
    friend class ThreadPool;
public:
    Job() 
    {
        ready_ = false;
        if(canceled_.init(true, false))
            if(completed_.init(true, true))
                ready_ = true;
    }

    virtual ~Job() {}

    void Cancel()
    {
        canceled_.Set();
    }

    bool Wait(uint32_t time)
    {
        if(ready_)
            return completed_.Wait(time);
        return true;
    }

protected:

    virtual void Do() {}

    bool Rest(uint32_t time)
    {
        if(ready_)
            return canceled_.WaitEx(time, true);
        return false;
    }

protected:
    bool ready_;
private:
    sys::Event canceled_;
    sys::Event completed_;
};

typedef std::tr1::shared_ptr<Job> JobPtr;


}
}

#endif